<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Common\Model;
use Think\Model;
/**
 * 基类model
 */
class UserModel extends BaseModel{
    /**
     * 添加用户
     * @param type $mobile 手机号
     * @param type $userpwd 用户密码
     * @param type $data
     */
    public function addUserList($mobile,$userpwd){
        
        $data = array();
        $data['userphone'] = $mobile;
        $data['userpwd'] = $userpwd;           
        $_validate = array(
            array('userphone', 'require', '手机号不能为空'),
            array('userpwd', 'require', '密码不能为空'),
        
        );
        
        if (!$this->validate($_validate)->create($data)) {
           
             $result = responseInfo('', $this->getError(),1);
             return $result;
             
        }
        
        $map['userphone'] = $data['userphone']; 
        $isUser = $this->where($map)->find();
        
        if(!empty($isUser)){

             $result = responseInfo('','用户已存在',1);
             return $result;
        }
            
        $data['md5pwd'] = md5($userpwd);
        $data['username'] = $mobile;
        $data['userphone'] = $mobile;
        $data['idfrom'] = getDeviceType();
        $data['createtime'] = time();
        $data['ip'] = get_client_ip();
        $data['isshow'] = 0;
        if($this->addData($data)){
            $result = responseInfo('','注册成功',0);
        }else{
            $result = responseInfo('','注册失败',1);
        }
        return $result;
    }

    /**
     * PC登录
     * @param $mobile 手机(账号)
     * @param $password 明文密码
     */
    public function getUserInfo($mobile='',$password=''){
    	$data = array();
    	$data['userphone'] = $mobile;
    	$data['userpwd'] = $password;

    	$_validate = array(
            array('userphone', 'require', '手机号不能为空'),
            array('userpwd', 'require', '密码不能为空'),
        );

        if (!$this->validate($_validate)->create($data)) {
           
            $result = responseInfo('', $this->getError(),1);
            return $result;            
        }

        $isExistence = $this->where($data)->find();

        if(empty($isExistence)){
            $result = responseInfo('','用户名或密码错误',1);
        }else{
        	$result = responseInfo('','登录成功',0);

			cookie('PCLogin', $mobile);
        }

        return $result;
    }
}